﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.CachTinhHocBongService;
namespace eTMS.Controllers{
public class CachTinhHocBongController : CachTinhHocBongControllerBase{
	public CachTinhHocBongController()
		: base()
	{
	}
    private DataTable _tblChiTietRemoved;
    private DataTable _tblChiTiet;
    public DataTable TableChiTiet
    {
        get
        {
            return _tblChiTiet;
        }
    }
    public void LoadChiTiet()
    {
        LoadChiTiet(CachTinhHocBongObject.CachTinhHocBong_ID);
    }
    private void LoadChiTiet(int CT_ID)
    {
        MucHocBongService.MucHocBongServiceClient wChiTiet =
            new MucHocBongService.MucHocBongServiceClient();
        _tblChiTiet = wChiTiet.SelectAllDSFilter("CachTinhHocBong_ID="+CT_ID).Tables[0];
        _tblChiTietRemoved = _tblChiTiet.Clone();
    }
    public void AddNewChiTiet()
    {
        if (_tblChiTiet == null)
        {
            LoadChiTiet(0);
        }
        DataRow row = _tblChiTiet.NewRow();
        row["DenDiem"] = 0;
        row["LayCanDuoi"] = false;
        row["LayCanTren"] = false;
        row["TuDiem"] = 0;
        row["SoTien"] = 0;
        row["LayCanDuoiRL"] = false;
        row["LayCanTrenRL"] = false;
        row["TuDiemRL"] = 0;
        row["SoTienRL"] = 0;
        _tblChiTiet.Rows.Add(row);
    }
    public void RemoveChiTiet(List<DataRow> list)
    {
        if (_tblChiTietRemoved == null) _tblChiTietRemoved = _tblChiTiet.Clone();
        foreach (DataRow row in list)
        {
            _tblChiTietRemoved.ImportRow(row);
            _tblChiTiet.Rows.Remove(row);
        }
    }
    public void SaveDetail()
    {
        MucHocBongService.MucHocBongServiceClient wChiTiet =
            new MucHocBongService.MucHocBongServiceClient();
        foreach (DataRow row in _tblChiTietRemoved.Rows)
        {
            int ID = ConvertInt(row["MucHocBong_ID"]);
            if (ID > 0)
                wChiTiet.DeleteByID(ID);
        }
        foreach (DataRow row in _tblChiTiet.Rows)
        {
            MucHocBongService.MucHocBongEntity chiTietEntity = new MucHocBongService.MucHocBongEntity();
            chiTietEntity.DenDiem = ConvertNumber(row["DenDiem"]);
            chiTietEntity.LayCanDuoi = ConvertBool(row["LayCanDuoi"]);
            chiTietEntity.LayCanTren = ConvertBool(row["LayCanTren"]);
            chiTietEntity.TuDiem = ConvertNumber(row["TuDiem"]);
            chiTietEntity.SoTien = ConvertNumber(row["SoTien"]);
            chiTietEntity.LayCanDuoiRL = ConvertBool(row["LayCanDuoiRL"]);
            chiTietEntity.LayCanTrenRL = ConvertBool(row["LayCanTrenRL"]);
            chiTietEntity.TuDiemRL = ConvertNumber(row["TuDiemRL"]);
            chiTietEntity.DenDiemRL = ConvertNumber(row["DenDiemRL"]);
            chiTietEntity.SoTien = ConvertNumber(row["SoTien"]);
            chiTietEntity.CachTinhHocBong_ID = CachTinhHocBongObject.CachTinhHocBong_ID;
            chiTietEntity.MucHocBong_ID = ConvertInt(row["MucHocBong_ID"]);
            chiTietEntity.TenMuc = row["TenMuc"].ToString();
            if (chiTietEntity.MucHocBong_ID > 0)
                wChiTiet.Update(chiTietEntity);
            else
                wChiTiet.Save(chiTietEntity);
        }
    }
}
}
